﻿using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;

namespace SynergyControls
{

 
    public class RhythmSettingsControl: SettingsUpdaterBase
    {
        #region Singleton Pattern

        private static RhythmSettingsControl instance;
        public static RhythmSettingsControl Instance
        {
            get
            {
                if (instance == null)
                    instance = new RhythmSettingsControl();
                return instance;
            }
        }

        #endregion Singleton Pattern

        #region Fields

        ComboBox numerator;
        ComboBox denominator;

        #endregion

        #region Dependency Properties

        #region _BeatsPerMeasure
        /// <suMMary>
        ///  /// <suMMary>
        /// Identifies the <see cref="_BeatsPerMeasure" /> dependency property. 
        /// </suMMary>
        /// </suMMary>
        public static readonly DependencyProperty BeatsPerMeasureProperty = DependencyProperty.Register("BeatsPerMeasure", typeof(int), typeof(RhythmSettingsControl), new PropertyMetadata(new PropertyChangedCallback(BeatsPerMeasureChanged)));

        private static void BeatsPerMeasureChanged(DependencyObject dependency, DependencyPropertyChangedEventArgs args)
        {
            RhythmSettingsControl instance = dependency as RhythmSettingsControl;
            if (instance != null)
            {
                instance.NotifyPropertyChanged("BeatsPerMeasure");
            }

        }
        

        [Category("Common")]
        public int BeatsPerMeasure
        {
            get
            {
                return (int)GetValue(BeatsPerMeasureProperty);
            }
            protected set
            {               
                SetValue(BeatsPerMeasureProperty, value);
            }
        }

        #endregion

        #region EqualsOneBeat


        /// <suMMary>
        ///  /// <suMMary>
        /// Identifies the <see cref="_BeatsPerMeasure" /> dependency property. 
        /// </suMMary>
        /// </suMMary>
        public static readonly DependencyProperty EqualsOneBeatProperty = DependencyProperty.Register("EqualsOneBeat", typeof(int), typeof(RhythmSettingsControl), new PropertyMetadata(new PropertyChangedCallback(EqualsOneBeatChanged)));

        private static void EqualsOneBeatChanged(DependencyObject dependency, DependencyPropertyChangedEventArgs args)
        {
            RhythmSettingsControl instance = dependency as RhythmSettingsControl;
            if (instance != null)
            {
                instance.NotifyPropertyChanged("EqualsOneBeat");
            }

        }
        

        [Category("Common")]
        public int EqualsOneBeat
        {
            get
            {
                return (int)GetValue(EqualsOneBeatProperty);
            }
            protected set
            {                
                SetValue(EqualsOneBeatProperty, value);
            }
        }

        #endregion

        #endregion

        #region Constructors

        static RhythmSettingsControl()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(RhythmSettingsControl), new FrameworkPropertyMetadata(typeof(RhythmSettingsControl)));
        }
        
        #endregion

        #region Event Overrides

        /// <summary>
        /// Don't route event handlers before handling the event in an OnApplyTemplate override.
        /// </summary>
        public override void OnApplyTemplate()
        {
            numerator = GetTemplateChild("NumeratorComboBox") as ComboBox;
            numerator.ItemsSource = Enum.GetValues(typeof(BeatsPerMeasureEnum));

            denominator = GetTemplateChild("DenominatorComboBox") as ComboBox;
            denominator.ItemsSource = Enum.GetValues(typeof(EqualsOneBeatEnum));

            numerator.SelectedItem = BeatsPerMeasureEnum.quadruple;
            numerator.SelectionChanged += numerator_SelectionChanged;
            
            denominator.SelectedItem = EqualsOneBeatEnum.simple;
            denominator.SelectionChanged += denominator_SelectionChanged;
            
        }

        void denominator_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int i = (int)denominator.SelectedValue;
            BeatsPerMeasure = i;
        }

        void numerator_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int i = (int)numerator.SelectedValue;
            EqualsOneBeat = i;
        }


        #endregion
    }
}
